package com.fengxinzi.activity.home.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.fengxinzi.R;
import com.fengxinzi.activity.user.UserActivity;
import com.fengxinzi.model.User;

public class HomeGridViewAdapter extends BaseAdapter {

	private Context mContext;
	private List<User> mList;
	private Animation mImgAnimation;
//	private Animation mFrameAnimation;
	
	public HomeGridViewAdapter(Context context , List<User> list) {
		super();
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.mList = list;
		mImgAnimation = AnimationUtils.loadAnimation(mContext, R.anim.griditem_zoom_img);
//		mFrameAnimation = AnimationUtils.loadAnimation(mContext, R.anim.griditem_zoom_frame);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList!=null?mList.size():0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList!=null ? mList.get(position):null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item_home, null);
			holder = new ViewHolder();
			holder.mImageView = (ImageView) convertView.findViewById(R.id.home_grid_item_img);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.mImageView.setImageResource(mList.get(position).getUserImg());
		holder.mImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				v.startAnimation(mImgAnimation);
				new Handler().postDelayed(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						Intent intent = new Intent(mContext,UserActivity.class);
						mContext.startActivity(intent);
					}
				}, 450);
			}
		});

		return convertView;
	}
	
	private class ViewHolder{
		private ImageView mImageView;
		private ImageView mFrameView;
	}
}
